/**
 * 
 */
package com.zxc.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.codis.jodis.JedisResourcePool;
import io.codis.jodis.RoundRobinJedisPool;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author admin
 *
 */

@WebServlet(name = "CodisTestServlet",urlPatterns = "/codisTest")
public class CodisTestServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5209204733355853985L;

	private JedisResourcePool jedisPool;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			
			List<String> l = new ArrayList<>();
			for (int j = 0; j < 20; j++) {
				l.add(j+"");
			}
			
			jedis.hmget("1", l.toArray(new String[0]));
			
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
	}

	@Override
	public void init() throws ServletException {
		jedisPool = buildPool();
	}
	
	private static JedisResourcePool buildPool(){
		JedisPoolConfig pool = new JedisPoolConfig();
		pool.setMinIdle(200);
		pool.setMaxIdle(200);
		pool.setMaxTotal(1000);
		pool.setMaxWaitMillis(3000);
		pool.setMinEvictableIdleTimeMillis(60000);
		pool.setSoftMinEvictableIdleTimeMillis(60000);
		pool.setTimeBetweenEvictionRunsMillis(1800000);
		pool.setNumTestsPerEvictionRun(1000);
		pool.setBlockWhenExhausted(false);
		pool.setTestOnBorrow(false);
		pool.setTestOnReturn(false);
		pool.setTestWhileIdle(true);
		
		return RoundRobinJedisPool
				.create()
				.curatorClient("192.168.18.100:2181", 30000)
				.zkProxyDir("/jodis/codis-demo")
				.poolConfig(pool)
				.database(0)
				.password("123456")
				.soTimeoutMs(3000)
				.build();
	}
}
